package com.yikaiye.android.yikaiye.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.databinding.ItemChooseCompanyBinding;
import com.yikaiye.android.yikaiye.model.CompanyRecordsModel;
import com.yikaiye.android.yikaiye.utils.CommonUtil;

import java.util.List;

public class ChooseCompanyAdapter extends BaseQuickAdapter<CompanyRecordsModel.CompanyModel, DataBindingHolder<ItemChooseCompanyBinding>> {
    private final Context context;
    private OnChildItemClickListener onChildItemClickListener;

    public ChooseCompanyAdapter(Context context) {
        this.context = context;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemChooseCompanyBinding> itemChooseCompanyBindingDataBindingHolder, int i, @Nullable CompanyRecordsModel.CompanyModel companyModel) {
        ItemChooseCompanyBinding binding = itemChooseCompanyBindingDataBindingHolder.getBinding();
        if (companyModel != null) {
            binding.tvCompanyName.setText(CommonUtil.checkText(companyModel.getEnterpriseName()));
            binding.tvCompanyCode.setText(context.getString(R.string.company_code, TextUtils.isEmpty(companyModel.getCreditNo()) || companyModel.getCreditNo().length() > 18 ? context.getString(R.string.no_data2) : companyModel.getCreditNo()));
            itemChooseCompanyBindingDataBindingHolder.itemView.setBackgroundResource(companyModel.isCheck() ? R.drawable.shape_ffffff_stroke_f93f50_4dp_bg : R.drawable.shape_ffffff_4dp);
            binding.ivChoose.setVisibility(companyModel.isCheck() ? View.VISIBLE : View.INVISIBLE);
            itemChooseCompanyBindingDataBindingHolder.itemView.setOnClickListener(view -> {
                List<CompanyRecordsModel.CompanyModel> items = getItems();
                for (CompanyRecordsModel.CompanyModel model : items) {
                    model.setCheck(false);
                }
                companyModel.setCheck(true);
                notifyDataSetChanged();
                onChildItemClickListener.onChildItemClickListener(companyModel);
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemChooseCompanyBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_choose_company, viewGroup);
    }

    public interface OnChildItemClickListener {
        void onChildItemClickListener(CompanyRecordsModel.CompanyModel companyModel);
    }

    public void setOnChildItemClickListener(OnChildItemClickListener onChildItemClickListener) {
        this.onChildItemClickListener = onChildItemClickListener;
    }
}
